package com.bnz.search.service.impl;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate;
import co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket;
import co.elastic.clients.elasticsearch._types.query_dsl.MultiMatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Highlight;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import com.alibaba.fastjson.JSON;
import com.bnz.search.entity.ItemEntity;
import com.bnz.search.entity.ItemVo;
import com.bnz.search.service.ItemSearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Service
//public class ItemSearchServiceImpl6 implements ItemSearchService {
//    @Autowired
//    private ElasticsearchClient client;
//    @Autowired
//    private StringRedisTemplate redisTemplate;
//
//    /**
//     * 1. 进行各种查询
//     *
//     * @param vo
//     * @return
//     * @throws IOException
//     */
//    @Override
//    public Map search(ItemVo vo) throws IOException {
//        //new map
//        Map resultMap = new HashMap();
//        //根据参数进行查询--关键词
//        if (vo != null) {
//            //定义查询到的商品列表
//            List<ItemEntity> itemList = new ArrayList<>();
//            //分类列表集合
//            List<String> categoryList = new ArrayList<>();
//            //如果不为null就进行查询。
//            if (StringUtils.isBlank(vo.getKeywords())) {
//
//            }
//            //
//        }
//        /**
//         * 根据分类名称在redis中查询规格及品牌列表
//         * @param category
//         * @return
//         */
//        private Map findBrandAndSpecList(){
//            //根据分类查模板id
//            BoundHashOperations<String, String, String> boundHashOperations = redisTemplate.boundHashOps("itemCatList");
//            String typeId = boundHashOperations.get(category);
//            if (typeId == null) {
//                typeId = "";
//            }
//            //根据模板id查品牌规格
//            String brandList = (String) redisTemplate.boundHashOps("brandList").get(typeId);
//            List<Map> brands = JSON.parseArray(StringUtils.isNotBlank(brandList) ? brandList : "[]", Map.class);
//            String specList = (String) redisTemplate.boundHashOps("specList").get(typeId);
//            List<Map> specs = JSON.parseArray(StringUtils.isNotBlank(specList) ? specList : "[]", Map.class);
//            //构造返回map
//            Map map = new HashMap();
//            //BrandEntity
//            //SpecificationEntity品牌及规格列表放到map中
//            map.put("specList", specs);
//            map.put("brandList", brands);
//            return map;
//
//        }
//    }
//}